package com.clothing.customization.repository;

import com.clothing.customization.entity.DesignerSchedule;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 设计师日程数据访问接口
 */
@Repository
public interface DesignerScheduleRepository extends JpaRepository<DesignerSchedule, Long>, JpaSpecificationExecutor<DesignerSchedule> {
    
    /**
     * 根据设计师ID查询日程列表
     */
    List<DesignerSchedule> findByDesignerIdOrderByStartTimeAsc(Long designerId);
    
    /**
     * 根据设计师ID和日程类型查询日程列表
     */
    List<DesignerSchedule> findByDesignerIdAndScheduleTypeOrderByStartTimeAsc(Long designerId, String scheduleType);
    
    /**
     * 根据设计师ID和状态查询日程列表
     */
    List<DesignerSchedule> findByDesignerIdAndStatusOrderByStartTimeAsc(Long designerId, Integer status);
    
    /**
     * 根据设计师ID和时间范围查询日程列表
     */
    List<DesignerSchedule> findByDesignerIdAndStartTimeBetweenOrderByStartTimeAsc(
            Long designerId, Date startTime, Date endTime);
    
    /**
     * 根据设计师ID、日程类型和时间范围查询日程列表
     */
    List<DesignerSchedule> findByDesignerIdAndScheduleTypeAndStartTimeBetweenOrderByStartTimeAsc(
            Long designerId, String scheduleType, Date startTime, Date endTime);
    
    /**
     * 根据条件分页查询日程信息
     */
    @Query("SELECT s FROM DesignerSchedule s WHERE " +
           "(:designerId IS NULL OR s.designerId = :designerId) AND " +
           "(:scheduleType IS NULL OR s.scheduleType = :scheduleType) AND " +
           "(:status IS NULL OR s.status = :status) AND " +
           "(:startTime IS NULL OR s.startTime >= :startTime) AND " +
           "(:endTime IS NULL OR s.endTime <= :endTime)")
    Page<DesignerSchedule> findByConditions(
            @Param("designerId") Long designerId,
            @Param("scheduleType") String scheduleType,
            @Param("status") Integer status,
            @Param("startTime") Date startTime,
            @Param("endTime") Date endTime,
            Pageable pageable);
    
    /**
     * 查询时间冲突的日程（用于检查新日程是否与已有日程冲突）
     */
    @Query("SELECT s FROM DesignerSchedule s WHERE " +
           "s.designerId = :designerId AND " +
           "s.status IN (0, 1) AND " + // 待开始或进行中的日程
           "((s.startTime <= :endTime AND s.endTime >= :startTime) OR " + // 时间段有交叉
           "(s.startTime >= :startTime AND s.endTime <= :endTime) OR " + // 完全包含在新日程内
           "(s.startTime <= :startTime AND s.endTime >= :endTime))") // 新日程完全包含在已有日程内
    List<DesignerSchedule> findConflictSchedules(
            @Param("designerId") Long designerId,
            @Param("startTime") Date startTime,
            @Param("endTime") Date endTime);
    
    /**
     * 统计各类型的日程数量
     */
    @Query("SELECT s.scheduleType, COUNT(s) FROM DesignerSchedule s WHERE s.designerId = :designerId GROUP BY s.scheduleType")
    List<Object[]> countByScheduleTypeGroup(@Param("designerId") Long designerId);
    
    /**
     * 统计各状态的日程数量
     */
    @Query("SELECT s.status, COUNT(s) FROM DesignerSchedule s WHERE s.designerId = :designerId GROUP BY s.status")
    List<Object[]> countByStatusGroup(@Param("designerId") Long designerId);
} 